package org.zjx.app.action.task;

import org.zjx.app.action.route.BaseRouteAction;
import org.zjx.app.action.route.RunningAction;
import org.zjx.command.entity.screen.Address;
import org.zjx.window.action.Action;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.List;
import java.util.Random;

public class PatrolTaskAction extends Action {
    private final List<Address> addresses;
    private final Random random = new Random();

    public PatrolTaskAction(IWindow targetWindow, List<Address> addresses) {
        super(targetWindow);
        this.addresses = addresses;
    }

    @Override
    protected void doExecute() throws Exception {
        if (!timeOut()) return;
        Address address = getRandomPoint();
        targetWindow.submitAction(new RunningAction(targetWindow, address));
        targetWindow.getState().getGameState().setCommand(null);
        targetWindow.getState().getGameState().setGatherQuest(false);
    }

    private boolean timeOut() {
        long before = stateManager.getGameState().getRecordAddressTime();
        return System.currentTimeMillis() - before > 3000;
    }

    /**
     * 随机获取一个点位
     *
     * @return 随机点位
     * @throws IllegalStateException 如果点位集合数据小于2
     */
    public Address getRandomPoint() {
        if (addresses == null || addresses.size() < 2) {
            throw new IllegalStateException("点位集合数据不足，至少需要2个点位才能进行巡逻");
        }
        return addresses.get(random.nextInt(addresses.size()));
    }
}
